package com.szxx.suit.bomb;

import java.util.LinkedList;
import java.util.List;

import org.springframework.stereotype.Component;

import com.szxx.constant.DdzCardTypes;
import com.szxx.domain.Card;
import com.szxx.domain.Cards;
import com.szxx.suit.AbstractSuitTerm;
import com.szxx.suit.AbstractSuitType;
import com.szxx.suit.DdzAnalysisResult;
import com.szxx.suit.DdzSuitAnalyzer;
import com.szxx.suit.DdzTypeUtil;

/**
 * 炸弹牌型 <br>
 *
 * @author Ambrose
 */
@Component
public class BombTypeAnalyzer extends DdzSuitAnalyzer {

	@Override
	public AbstractSuitType getSuitType() {
		return DdzCardTypes.BOMB;
	}

	/** 关键数量 */
	public static int TUPLE_SIZE = 4;

	@Override
	public boolean doCheckRule(Cards cards, AbstractSuitTerm term, DdzAnalysisResult result) {
		if (result.getCardNum() == TUPLE_SIZE) {
			if (result.getLaziCardNum() == 0 || result.isAllCardLazi()) { // 没有癞子，都是癞子
				return result.getAllValueCounter().getValuesInCount(TUPLE_SIZE).size() > 0;
			} else { // 癞子的情况
				return false;
			}
		} else {
			return false;
		}
	}

	@Override
	protected List<Cards> doAnalysizeLaizi(Cards cards, AbstractSuitTerm term, boolean onlyMaxValue) {
		List<Cards> cardsResult = new LinkedList<Cards>();
		Cards cardsCopy = cards.clone();
		analysize(cardsCopy, term);
		cardsResult.add(cardsCopy);
		return cardsResult;
	}

	@Override
	protected boolean containsPossibleCards(Cards handCards, AbstractSuitTerm term) {
		DdzAnalysisResult result = (DdzAnalysisResult) handCards.getResult();
		if (result.getCardNum() >= TUPLE_SIZE) {
			return result.getAllValueCounter().getValuesInCount(TUPLE_SIZE).size() > 0;
		} else {
			return false;
		}
	}

	@Override
	protected List<Cards> doListPossibleCards(Cards handCards, AbstractSuitTerm term) {
		List<Cards> cardsResult = new LinkedList<Cards>();
		DdzAnalysisResult result = (DdzAnalysisResult) handCards.getResult();
		List<Card> noneLaiziCardList = handCards.subList(0, result.getNoneCardNum());
		Cards noneLaiziCards = DdzTypeUtil.createCardsCopy(noneLaiziCardList);
		noneLaiziCards.setResult(handCards.getResult().clone());

		// 非癞子部分寻找炸弹
		List<Cards> count_4_cardsList = DdzTypeUtil.selectTupleCountCards(noneLaiziCards, TUPLE_SIZE);
		for (Cards cardsCopy : count_4_cardsList) {
			if (match(cardsCopy, term)) {
				analysize(cardsCopy, term);
				cardsResult.add(cardsCopy);
			}
		}
		if (result.getLaziCardNum() >= TUPLE_SIZE) { // 考虑癞子
			List<Card> laiziCardList = handCards.subList(result.getNoneCardNum(), result.getCardNum());
			Cards laiziCards = DdzTypeUtil.createCardsCopy(laiziCardList);
			laiziCards.setResult(handCards.getResult().clone());
			// 癞子部分寻找炸弹
			List<Cards> count_4_laiziCardsList = DdzTypeUtil.selectTupleCountCards(laiziCards, TUPLE_SIZE);
			for (Cards cardsCopy : count_4_laiziCardsList) {
				if (match(cardsCopy, term)) {
					analysize(cardsCopy, term);
					cardsResult.add(cardsCopy);
				}
			}
		}
		return cardsResult;
	}

}
